package cn.com.zs.android.util;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import cn.com.zs.android.R;
import cn.com.zs.android.model.GameParams;
import cn.com.zs.android.model.PhoneParams;

/**
 * 绘图工具类
 * @author 曾帅
 */
public class Image {
	
	private Canvas canvas;
	private Resources resources;
	
	/**
	 * 初始化参数
	 * @param canvas
	 * @param resources
	 */
	public Image(Canvas canvas, Resources resources) {
		this.canvas = canvas;
		this.resources = resources;
	}

	/**
	 * 根据参数绘图
	 * @param whichHole		画哪几个洞
	 * @param status		画第几种状态
	 * @throws InterruptedException
	 */
	public void DrawImage(int[] whichHole, int[] status) throws InterruptedException {
		this.backgroundImage();
		for (int i = 0; i < whichHole.length; i ++) {
			this.getImage(status[i], HolePlace.getDesignationPlace(whichHole[i]));
		}
	}
	
	/**
	 * 画背景
	 */
	public void backgroundImage() {
		// 背景
		Drawable image = resources.getDrawable(R.drawable.background);
		image.setAlpha(GameParams.backgrouudAlpha);
        image.setBounds(0, 0, PhoneParams.width, PhoneParams.height);
        image.draw(canvas);
        // 音效
        this.voiceImage();
        // 进度条、分数
        this.gameTimeAndScoreImage();
        // 地洞
//        for(int i = 0; i < Constants.HOLE_TOTAL; i ++) {
//        	this.getImage(Constants.EMPTY_HOLE_STATUS, HolePlace.getDesignationPlace(i + 1));
//        }
	}
	
	/**
	 * 画倒计时
	 */
	public void countdownImage(long readyTime) {
		Drawable image = null;
		switch ((int) readyTime) {
		case 1:
			image = resources.getDrawable(R.drawable.readytime_one);
			break;
		case 2:
			image = resources.getDrawable(R.drawable.readytime_two);
			break;
		case 3:
			image = resources.getDrawable(R.drawable.readytime_three);
			break;
		default:
			image = resources.getDrawable(R.drawable.readytime_three);
			break;
		}
		int left  = PhoneParams.width / 2 - Constants.READY_NUMBER_SIZE / 2;
		int top = PhoneParams.height / 2 - Constants.READY_NUMBER_SIZE / 2;
		System.out.println(left);
		image.setBounds(left, top, left + Constants.READY_NUMBER_SIZE, top + Constants.READY_NUMBER_SIZE);
		image.draw(canvas);
	}
	
	/**
	 * 画洞或者小鱼
	 * @param status	画哪种状态
	 * @param holePlace	绘画的职位
	 */
	public void getImage(int status, int[] holePlace) {
		Drawable image = null;
		switch (status) {
			case Constants.EMPTY_HOLE_STATUS:
//				image = resources.getDrawable(R.drawable.empty);
				break;
			case Constants.HAMSTER_STATUS_START:
				image = resources.getDrawable(R.drawable.little_up);
				break;
			case 2:
				image = resources.getDrawable(R.drawable.low_up);
				break;
			case 3:
				image = resources.getDrawable(R.drawable.high);
				break;
			case 4:
				image = resources.getDrawable(R.drawable.low_down);
				break;
			case Constants.HAMSTER_STATUS_END:
				image = resources.getDrawable(R.drawable.little_down);
				break;
			case Constants.HAMSTER_STATUS_HIT:
				image = resources.getDrawable(R.drawable.hit);
				break;
			default:
				break;
		}
		if (image == null || holePlace == null || holePlace.length == 0) {
			return;
		}
		image.setAlpha(GameParams.backgrouudAlpha);
		image.setBounds(holePlace[0], holePlace[1], holePlace[2], holePlace[3]);
		image.draw(canvas);
	}
	
	/**
	 * 画音效
	 */
	public void voiceImage() {
		Drawable background_music;
		if(GameParams.isBackgroundMusic) {
			background_music = resources.getDrawable(R.drawable.default_background_music);
		} else {
			background_music = resources.getDrawable(R.drawable.close_background_music);
		}
        background_music.setBounds(Constants.BACKGROUND_MUSIC[0], Constants.BACKGROUND_MUSIC[1], Constants.BACKGROUND_MUSIC[2], Constants.BACKGROUND_MUSIC[3]);
        background_music.draw(canvas);
        
        Drawable system_sound;
        if(GameParams.isSystemSound) {
        	system_sound = resources.getDrawable(R.drawable.default_system_sound);
		} else {
			system_sound = resources.getDrawable(R.drawable.close_system_sound);
		}
        system_sound.setBounds(Constants.SYSTEM_SOUND[0], Constants.SYSTEM_SOUND[1], Constants.SYSTEM_SOUND[2], Constants.SYSTEM_SOUND[3]);
        system_sound.draw(canvas);
	}
	
	/**
	 * 画进度条（游戏时间）及分数
	 */
	public void gameTimeAndScoreImage() {
		Drawable gameTime_image = resources.getDrawable(GameUtil.getGameTimeImage());
		gameTime_image.setBounds(Constants.GAME_TIME[0], Constants.GAME_TIME[1], Constants.GAME_TIME[2], Constants.GAME_TIME[3]);
		gameTime_image.draw(canvas);
		
		Drawable gameScore_image = resources.getDrawable(R.drawable.score_bucket);
		gameScore_image.setBounds(Constants.GAME_SCORE[0], Constants.GAME_SCORE[1], Constants.GAME_SCORE[2], Constants.GAME_SCORE[3]);
		gameScore_image.draw(canvas);
	}
	
	/**
	 * 画GameOver
	 */
	public void gameOverImage() {
		Drawable image = resources.getDrawable(R.drawable.gameover);
		int[] holePlace = HolePlace.getDesignationPlace(Constants.GAME_OVER_STATUS);
		image.setBounds(holePlace[0], holePlace[1], holePlace[2], holePlace[3]);
		image.draw(canvas);
	}
	
	/**
	 * 画GameRestart
	 */
	public void gameRestartImage() {
		Drawable image = resources.getDrawable(R.drawable.restart);
		int[] holePlace = HolePlace.getDesignationPlace(Constants.GAME_RESTART_STATUS);
		image.setBounds(holePlace[0], holePlace[1], holePlace[2], holePlace[3]);
		image.draw(canvas);
	}
	
}
